<template>
    <div>
        <div style="padding: 10px;">

            <!--            搜索区域-->
            <div style="margin:10px 0;">
                <el-input v-model="search" placeholder="请输入关键字" style="width: 20%;" clearable/>
                <el-button type="primary" style="margin-left: 5px;" @click="load"> 查询</el-button>
            </div>
            <el-table
                    :data="tableData"
                    style="width: 100%;"
                    stripe
            >
                <el-table-column prop="paperName" label="试卷名称" width="100"></el-table-column>
                <el-table-column prop="score" label="总分" width="100"></el-table-column>
<!--                <el-table-column prop="courseId" label="课程ID" width="100"></el-table-column>-->
                <el-table-column prop="courseName" label="课程名称" width="100"></el-table-column>
                <el-table-column prop="makePaperMethod" label="组卷方式" width="100">
                    <template slot-scope="scope">
                        <el-tag type="primary" v-if="scope.row.makePaperMethod === 1">固定组卷</el-tag>
                        <el-tag type="warning" v-if="scope.row.makePaperMethod === 2">随机组卷</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="questionCount" label="题目数量" width="100"></el-table-column>
                <el-table-column prop="suggestTime" label="考试时长" width="100"></el-table-column>
                <el-table-column prop="limitStartTime" label="开始时间" :formatter="formatDate" width="120"></el-table-column>
                <el-table-column prop="limitEndTime" label="结束时间" :formatter="formatDate" width="120"></el-table-column>
                <el-table-column prop="teacherName" label="出题老师" width="100"></el-table-column>
                <!--操作-->
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-popconfirm
                                class="ml-5"
                                confirm-button-text='确定'
                                cancel-button-text='取消'
                                icon="el-icon-info"
                                icon-color="red"
                                title="准备开始考试？"
                                @confirm="exam(scope.$index,scope.row)"
                        >
                            <el-button type="danger" size="small" slot="reference"><i
                                    class="el-icon-remove-outline"></i> 去考试
                            </el-button>
                        </el-popconfirm>

                    </template>
                </el-table-column>
            </el-table>
            <!--            分页栏-->
            <div style="margin: 10px 0;">
                <el-pagination
                        v-model:pageNum="pageNum"
                        :page-sizes="[5, 10, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                >
                </el-pagination>
            </div>

        </div>
    </div>
</template>

<script>

    import request from "../utils/request";
    import {serverIp} from "../../public/config";

    export default {
        name: 'Paper',
        components: {},
        data() {
            return {
                form: {},
                roles: {},
                serverIp: serverIp,
                dialogVisible: false,
                search: '',
                pageSize: 5,
                pageNum: 1,
                total: 20,
                tableData: [],
                status: [{name: "启用", value: "1"}, {name: "禁用", value: "0"}],
                rules: {
                    paperId: [
                        {required: true, message: '请输入用戶ID', trigger: 'blur'},
                        {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ]
                }
            }
        },
        created() {
            this.load()
        },
        methods: {
            load() {
                // get方法需要params，post不要 "http://localhost:9090/paper"
                request.get("/paper", {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        paperName: this.search,
                    }
                }).then(res => {
                    console.log("res:", res);
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                })
            },
            add() {
                this.dialogVisible = true;
                this.form = {};
                this.form.useStatus = "1"; //初始化使用状态
            },
            save() {
                console.log("form:", this.form)
                if (this.form.id) { // 更新
                    request.post('/paper/save', this.form).then(res => {
                        console.log("code:", res.code)
                        if (res.code == '200') {
                            this.$message.success("保存成功")
                        } else {
                            this.$message.error("保存失败")
                        }
                        this.load() //刷新表格
                        this.dialogVisible = false
                        console.log("success")
                    })
                } else { //新增
                    request.post("/paper/save", this.form).then(res => {
                        console.log(res.data)
                        if (res.code == '200') {
                            this.$message.success("添加成功")
                        } else {
                            this.$message.error("添加失败")
                        }
                        this.load() //刷新表格
                        this.dialogVisible = false
                        console.log("success")
                    })
                }

            },
            handleSizeChange(pageSize) { //改变当前每页的个数触发
                this.pageSize = pageSize
                console.log(pageSize)
                this.load()
            },
            handleCurrentChange(pageNum) {  //改变当前页码触发
                this.pageNum = pageNum
                this.load()
            },
            formatDate(row, column) {
                // 获取单元格数据
                let data = row[column.property]
                if(data == null) {
                    return null
                }
                let dt = new Date(data)
                return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds()
            },
            exam(index,row){
                console.log("row",row);
                this.$router.push({ path:"/exam/do", name: 'Do', params: { paper: row }})
            }

        }

    }
</script>
